package com.bs.sc.base.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MemoryIDMigrator {

	private static MessageDigest md5Digest;

	static {
		try {
			md5Digest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}

	public static long toLongID(String stringID) {
		try {
			return hash(stringID);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return 0;
	}

	private final static long hash(String value) throws UnsupportedEncodingException {
		byte[] md5hash = null;
		synchronized (MemoryIDMigrator.md5Digest) {
			md5hash = MemoryIDMigrator.md5Digest.digest(value.getBytes("UTF-8"));
			MemoryIDMigrator.md5Digest.reset();
		}
		long hash = 0L;
		for (int i = 0; i < 8; i++) {
			hash = hash << 8 | md5hash[i] & 0xFF;
		}
		return hash;
	}
}
